import Big from 'big.js'
// 转换时长
function getDuration (second) {
  if (!second) {
    return '0秒'
  }
  let isNegative = false
  if (second < 0) {
    isNegative = true
    second = new Big(second).abs()
  } else {
    second = new Big(second)
  }
  const days = Math.floor(second.div(86400).toFixed(1))
  const hours = Math.floor(second.mod(86400).div(3600).toFixed(1))
  const minutes = Math.floor(second.mod(86400).mod(3600).div(60).toFixed(1))
  const seconds = second.mod(86400).mod(3600).mod(60).toFixed(0)
  var duration = getCovering(days, '天') + getCovering(hours, '小时') + getCovering(minutes, '分') + getCovering(seconds, '秒')
  duration !== '' && isNegative && (duration = '-' + duration)
  return duration
}

function getCovering (num, label = '', isNull = '') {
  if (num && num > 0) {
    // num < 10 && (num = `0${num}`)
    return `${num}${label}`
  } else {
    return isNull
  }
}

export default {
  getDuration
}
